using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Pbdms.NetworkDealer.Utilities
{
    /// <summary>
    /// this class's role is to send any message to specified target,
    /// </summary>
    public class Sender
    {
        /// <summary>
        /// arguments contain information of what will be sent and to whom
        /// </summary>
        object[] args;
        public Sender(object[] args)
        {
            this.args = args;
        }
        /// <summary>
        /// send a message to the network
        /// * Note: always create a seperate thread for using this method, 
        /// since after sending the message, it will abort the current thread
        /// </summary>
        public void sendMessagetoNetwork()
        {
            //to send a message to network, the format of the args must be
            //args[0] is the string message to be sent
            //args[1] is the IPAddress object representing the IPAddress of the destination
            string msg = (string)args[0];
            IPAddress ipAdd = (IPAddress)args[1];
            IPEndPoint endPoint = new IPEndPoint(ipAdd, NetworkDealer.LISTEN_PORT );
            Socket sk;
            try
            {
                sk = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(msg);
                // sk.Send(dataBuffer);
                sk.Connect(endPoint);
                sk.Send(dataBuffer);
                sk.Close();

            }
            catch (SocketException)
            {
            }
            finally
            {
                System.Threading.Thread.CurrentThread.Abort();
            }
        }
        /// <summary>
        /// start sending a file to the client who has requested it
        /// </summary>
        public void sendFileToClient()
        {
            ////wait for a sec for the connection to be established
            System.Threading.Thread.CurrentThread.Join(1000);
            //send the file
            IPAddress clientIpAdd= IPAddress.Parse((String)(args[0]));
            int clientPort = (int)(args[1]);
            string filePath = (string)(args[2]);

            IPEndPoint endPoint = new IPEndPoint(clientIpAdd, clientPort);
            Socket sk;
            try
            {
                sk = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                //connect to client
                sk.Connect(endPoint);
                //start reading the file
                System.IO.FileInfo fInfo = new System.IO.FileInfo(filePath);
                
                NetworkStream nfs = new NetworkStream(sk);

                System.IO.FileStream fin = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                long rdby = 0;
                long total = fin.Length;
                int len = 0;
                byte[] buffed = new byte[1024];

                while (rdby < total && nfs.CanWrite)
                {
                    //Read from the File (len contains the number of bytes read)
                    len = fin.Read(buffed, 0, buffed.Length);
                    //Write the Bytes on the Socket
                    nfs.Write(buffed, 0, len);
                    //Increase the bytes Read counter
                    rdby = rdby + len;

                    int pc = (int)(((double)rdby / (double)total) * 100.00);
                    string perc = pc.ToString() + "%";                    
                }

                fin.Close();

            }
            catch (SocketException)
            {
            }
            finally
            {
                System.Threading.Thread.CurrentThread.Abort();
            }

        }
    }
}
